package com.vancouver.knows.ui.fragment.citydiscount;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.OnClick;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.pangu.ui.browser.LoadableList;
import com.vancouver.knows.LocationController;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.ui.activity.citydiscount.MapActivity;
import com.vancouver.knows.ui.activity.search.NewSearchActivity;
import com.vancouver.knows.ui.base.BaseFragment;
import com.vancouver.knows.ui.view.compactcalendarview.AnimatorListener;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.task.mark.FetchCityDiscountsTaskMark;

public class CityDiscountFragment extends BaseFragment implements LoadableList.LoadableListScrollListener {

	@Bind(R.id.toolbar)
	Toolbar mToolbar;
	@Bind(R.id.cityDiscounts)
	CityDiscountPullControllerView cityDisControllerView;

	private LocationController mLocationController;

	public static CityDiscountFragment newInstance() {
		return new CityDiscountFragment();
	}

	@Override
	protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mLocationController = new LocationController(getActivity(),true);
		mLocationController.onCreate();
		return inflater.inflate(R.layout.activity_citydiscounts, container, false);
	}

	@Override
	public void onStart() {
		mLocationController.onStart();
		super.onStart();
	}

	@Override
	public void onStop() {
		mLocationController.onStop();
		super.onStop();
	}

	@Override
	public void subHandleMessage(Message msg) {
		switch (msg.what) {
			case Constants.BUSINESS_STATUS_CITYDISCOUNT_COMMENT_COUNT_CHANGE:
				cityDisControllerView.flush();
				break;
		}
	}

	@Override
	public void onResume() {
		super.onResume();

		Tracker t = ((MainApplication) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
		t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_CITYDISCOUNT_FRAGMENT);
		t.send(new HitBuilders.ScreenViewBuilder().build());
	}

	@OnClick(R.id.search_icon)
	public void onSearchIconClick() {
		Intent searchIntent = new Intent(getActivity(), NewSearchActivity.class);
		startActivity(searchIntent);
	}

	@OnClick(R.id.map_icon)
	public void onMapIconClick() {
		if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())== ConnectionResult.SUCCESS){
			Intent intent = new Intent(getActivity(), MapActivity.class);
			startActivity(intent);
		}else{
			Toast.makeText(getActivity(),R.string.install_google_play_services,200).show();
		}

	}

	@Override
	protected void bindView(View rootView) {
		MainApplication imContext = ((MainApplication) getActivity().getApplication());
		FetchCityDiscountsTaskMark aTaskMark = imContext.getBusinessModule().getTaskMarkPool()
				.getFetchCityDiscountsTaskMark();
		cityDisControllerView.loadData(aTaskMark);
		cityDisControllerView.setQrCode(AppUtils.createQrImage(getActivity()));
		//cityDisControllerView.setScrollListener(this);
	}

	public void scrollUp() {
		cityDisControllerView.scrollUp();
	}

	@Override
	public void onScrollUp() {
		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
		viewPropertyAnimator.setListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
				mToolbar.setVisibility(View.VISIBLE);
			}
		});
		viewPropertyAnimator.translationY(0).setInterpolator(new AccelerateInterpolator()).start();
	}

	@Override
	public void onScrollDown() {
		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
		viewPropertyAnimator.setListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
				mToolbar.setVisibility(View.GONE);
			}
		});
		viewPropertyAnimator.translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
	}
}
